/**
*FileContentManager is a Java based file manager desktop application, 
*it can show, edit and manipulate the content of the files archived inside a zip.
*
*Copyright (C) 2007 
*
*Created by Camila Sanchez [http://mimix.wordpress.com/], Rafael Naufal [http://rnaufal.livejournal.com] and Rodrigo [rdomartins@gmail.com]
*
*FileContentManager is free software; you can redistribute it and/or
*modify it under the terms of the GNU Lesser General Public
*License as published by the Free Software Foundation; either
*version 2.1 of the License, or (at your option) any later version.
*
*This library is distributed in the hope that it will be useful,
*but WITHOUT ANY WARRANTY; without even the implied warranty of
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
*Lesser General Public License for more details.
*
*You should have received a copy of the GNU Lesser General Public
*License along with FileContentManager; if not, write to the Free Software
*Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
 * Created on 31/05/2007 18:12:25
 */
package br.com.jteam.jfcm.model.util;

import java.io.File;

/**
 * <p>
 * Respons&aacute;vel por ser um utilit&aacute;rio na leitura e abertura de
 * arquivos.
 * 
 * @author Rafael Naufal <rafael.naufal@gmail.com>
 * @version $Id$
 */
public class FileTypeUtil
{
	/**
	 * <p>
	 * Contrutor privado.
	 * 
	 */
	private FileTypeUtil()
	{
		super();
	}

	/**
	 * <p>
	 * Extrai a extens&atilde;o de um arquivo.
	 * 
	 * @param fileName -
	 *            nome do arquivo.
	 * @return a extens&atilde;o do arquivo. Se o arquivo n&atilde;o possuir
	 *         nenhuma extens&atilde;o, retorna nulo.
	 * @throws IllegalArgumentException
	 *             caso o nome do arquivo seja nulo.
	 */
	public static String extractFileExtension(String fileName)
	{
		if (fileName == null)
		{
			throw new IllegalArgumentException("File name cannot be null");
		}
		if (!fileName.contains("."))
		{
			return null;
		}
		return fileName.substring(fileName.lastIndexOf("."));
	}

	/**
	 * <p>
	 * Verifica se o arquivo est&aacute; presente no disco.
	 * 
	 * @param filePath -
	 *            caminho do arquivo.
	 * @return - <code>true</code> se o arquivo estiver presente no disco,
	 *         <code>false</code> em caso contr&aacute;rio.
	 */
	public static boolean isFilePresentOnDisk(String filePath)
	{
		if (filePath == null)
		{
			throw new IllegalArgumentException("File name cannot be null");
		}
		File file = new File(filePath);
		return file.exists();
	}

	public static String extractParentNamePath(String childFileName)
	{
		if (childFileName == null)
		{
			return null;
		}
		if (!childFileName.contains("/"))
		{
			return childFileName;
		}
		if (childFileName.indexOf("/") == childFileName.length() - 1)
		{
			return childFileName.replace("/", "");
		}
		String fileName[] = childFileName.split("/");
		return fileName[fileName.length - 1];
	}

	public static String adaptParentFileName(String fileName)
	{
		if (fileName == null)
		{
			return null;
		}
		if (!fileName.contains("/"))
		{
			return fileName;
		}
		String separator = System.getProperty("file.separator");
		String newParentFileName = fileName.replace('/', '\\');
		if (newParentFileName.endsWith(separator))
		{
			return newParentFileName.substring(	0,
												newParentFileName.length() - 1);
		}
		return newParentFileName;
	}
}
